package apis

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/go-admin-team/go-admin-core/sdk/api"
	"github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
	"tuowei.com/app/api/models"
	"tuowei.com/app/api/service"
	"tuowei.com/app/api/service/dto"
	"tuowei.com/common/actions"
	"tuowei.com/common/global"
)

type ApiAddress struct {
	api.Api
}

func (e ApiAddress) GetPages(c *gin.Context) {
	s := service.SysAddress{}
	req := dto.SysAddressGetPageReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	//从token中获取id
	if req.StudentId == 0 {
		req.StudentId = user.GetUserId(c)
	}
	//数据权限检查
	p := actions.GetPermissionFromContext(c)

	list := make([]models.SysAddress, 0)

	err = s.GetPage(&req, p, &list)
	if err != nil {
		e.Error(500, err, "查询失败")
		return
	}
	e.OK(list, "查询成功")
}

func (e ApiAddress) Insert(c *gin.Context) {
	s := service.SysAddress{}
	req := dto.SysAddressInsertReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.JSON).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	//从token中获取id
	if req.StudentId == 0 {
		req.StudentId = user.GetUserId(c)
	}
	// 设置创建人
	req.SetCreateBy(user.GetUserId(c), user.GetUserName(c))
	if req.DefaultAddress == 1 {
		//默认唯一
		s1 := service.SysAddress{}
		s1.Orm = s.Orm
		req1 := dto.SysAddressGetPageReq{}
		req1.StudentId = req.StudentId
		if req.StudentId == 0 {
			req.StudentId = user.GetUserId(c)
		}
		//数据权限检查
		p := actions.GetPermissionFromContext(c)
		list := make([]models.SysAddress, 0)
		err = s1.GetPage(&req1, p, &list)
		if err != nil {
			e.Error(500, err, "查询失败")
			return
		}
		for _, l := range list {
			if l.DefaultAddress == 1 {
				s := service.SysAddress{}
				s.Orm = s1.Orm
				req := dto.SysAddressUpdateReq{}
				req.Id = l.Id
				req.DefaultAddress = 2
				req.SetUpdateBy(user.GetUserId(c), user.GetUserName(c))
				err = s.Update(&req)
				if err != nil {
					e.Error(500, err, "更新失败")
					return
				}
				break
			}

		}
	}

	err = s.Insert(&req)
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	e.OK(req.GetId(), "创建成功")
}

func (e ApiAddress) Change(c *gin.Context) {
	s := service.SysAddress{}
	req := dto.SysAddressUpdateReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.JSON, nil).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	//从token中获取id
	req.SetUpdateBy(user.GetUserId(c), user.GetUserName(c))
	if req.DefaultAddress == 1 {
		//默认唯一
		s1 := service.SysAddress{}
		s1.Orm = s.Orm
		//
		listo := models.SysAddress{}
		err = s1.GetAddress(req.Id, &listo)
		if err != nil {
			e.Error(500, err, "查询失败")
			return
		}
		//
		req1 := dto.SysAddressGetPageReq{}
		req1.StudentId = listo.StudentId
		//数据权限检查
		p := actions.GetPermissionFromContext(c)
		list := make([]models.SysAddress, 0)
		err = s1.GetPage(&req1, p, &list)
		if err != nil {
			e.Error(500, err, "查询失败")
			return
		}
		for _, l := range list {
			if l.DefaultAddress == 1 {
				s := service.SysAddress{}
				s.Orm = s1.Orm
				req := dto.SysAddressUpdateReq{}
				req.Id = l.Id
				req.DefaultAddress = 2
				req.SetUpdateBy(user.GetUserId(c), user.GetUserName(c))
				err = s.Update(&req)
				if err != nil {
					e.Error(500, err, "更新失败")
					return
				}
				break
			}

		}
	}

	err = s.Update(&req)
	if err != nil {
		e.Error(500, err, "更新失败")
		return
	}
	e.OK(req.GetId(), "更新成功")
}

func (e ApiAddress) Delete(c *gin.Context) {
	s := new(service.SysAddress)
	req := dto.SysAddressDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.JSON).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, fmt.Sprintf("删除角色 %v 失败，\r\n失败信息 %s", req.Id, err.Error()))
		return
	}

	err = s.Remove(&req)
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, "")
		return
	}
	_, err = global.LoadPolicy(c)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("删除角色 %v 失败，失败信息 %s", req.GetId(), err.Error()))
		return
	}
	e.OK(req.GetId(), fmt.Sprintf("删除角色角色 %v 状态成功！", req.GetId()))
}
